<div class="wrapper clearfix">
	<div class="position mt_10"> <span>您当前的位置：</span> <a href="{url:}"> 首页</a> » 购物车</div>
	<div class="myshopping m_10">
	  <ul class="order_step">
		<li class="current"><span class="first">1、查看购物车</span></li>
		<li><span>2、填写核对订单信息</span></li>
		<li class="last"><span>3、成功提交订单</span></li>
	  </ul>
	</div>

	<div class="cart_prompt f14 t_l" {if:empty($this->promotion)}style="display:none"{/if}>
		<p class="m_10 gray"><b class="orange">恭喜，</b>您的订单已经满足了以下优惠活动！</p>
		{foreach:items = $this->promotion}
		<p class="indent blue">{$item['plan']}，{$item['info']}</p>
		{/foreach}
	</div>

	<table width="100%" class="cart_table m_10">
		<col width="115px" />
		<col />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<col width="80px" />
		<caption>查看购物车</caption>
		<thead>
			<tr><th>图片</th><th>商品名称</th><th>赠送积分</th><th>单价</th><th>优惠</th><th>数量</th><th>小计</th><th class="last">操作</th></tr>
		</thead>
		<tbody>
			{set:$goodsIdArray = array()}
			{foreach:items = $this->productList}
			{set:$goodsIdArray[] = $item['goods_id']}
			<tr>
				<td><img src="{webroot:$item[list_img]}" width="66" height="66" alt="{$item['name']}" title="{$item['name']}" /></td>
				<td class="t_l">
					<a href="{url:/site/products/id/$item[goods_id]}" class="blue">{$item['name']}</a>
					<p>
					{set:$spec_array=Block::show_spec($item['spec_array']);}
					{foreach:items=$spec_array item=$specValue key=$specName}
						{$specName}：{$specValue} &nbsp&nbsp
					{/foreach}
					</p>
				</td>
				<td>{$item['point']}</td>
				<td><b>￥{$item['sell_price']}</b></td>
				<td>减￥{$item['reduce']}</td>
				<td>
					<div class="num">
						{set:unset($item['name']);$item_json = JSON::encode($item)}
						<a class="reduce" href="javascript:void(0)" onclick='cart_reduce("product",{$item_json});'>-</a>
						<input class="tiny" value="{$item['count']}" onblur='cartCount("product",{$item_json});' type="text" id="product_count_{$item['id']}" />
						<a class="add" href="javascript:void(0)" onclick='cart_increase("product",{$item_json});'>+</a>
					</div>
				</td>
				<td>￥<b class="red2" id="product_sum_{$item['id']}">{$item['sum']}</b></td>
				<td><a href="{url:/simple/removeCart/link/cart/type/product/goods_id/$item[id]}">删除</a></td>
			</tr>
			{/foreach}

			{foreach:items = $this->goodsList}
			{set:$goodsIdArray[] = $item['id']}
			<tr>
				<td><img src="{webroot:$item[list_img]}" width="66px" height="66px" alt="{$item['name']}" title="{$item['name']}" /></td>
				<td class="t_l">
					<a href="{url:/site/products/id/$item[id]}" class="blue">{$item['name']}</a>
				</td>
				<td>{$item['point']}</td>
				<td>￥<b>{$item['sell_price']}</b></td>
				<td>减￥{$item['reduce']}</td>
				<td>
					<div class="num">
						{set:unset($item['name']);$item_json = JSON::encode($item)}
						<a class="reduce" href="javascript:void(0)" onclick='cart_reduce("goods",{$item_json});'>-</a>
						<input class="tiny" value="{$item['count']}" onblur='cartCount("goods",{$item_json});'  type="text" id="goods_count_{$item['id']}">
						<a class="add" href="javascript:void(0)" onclick='cart_increase("goods",{$item_json});'>+</a>
					</div>
				</td>
				<td>￥<b class="red2" id="goods_sum_{$item['id']}">{$item['sum']}</b></td>
				<td><a href="{url:/simple/removeCart/link/cart/type/goods/goods_id/$item[id]}">删除</a></td>
			</tr>
			{/foreach}

			<tr class="stats">
				<td colspan="8">
					<span>商品总重量：<b id='weight'>{$this->weight}</b>g</span><span>商品总金额：￥<b id='origin_price'>{$this->sum}</b> - 商品优惠：￥<b id='discount_price'>{$this->reduce}</b> - 促销活动优惠：￥<b id='promotion_price'>{$this->proReduce}</b></span><br />
					金额总计（不含运费）：￥<b class="orange" id='sum_price'>{$this->final_sum}</b>
				</td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2" class="t_l">
					{if:$this->user['user_id']}
					<a class="file" href="{url:/simple/deposit_cart_set}">寄存购物车</a>
					<a class="file" href="javascript:deposit_cart_get();">取出购物车</a>
					<a class="file" href="javascript:void(0)" onclick="delModel({msg:'确定要清空寄存购物车么？',link:'{url:/simple/deposit_cart_clear}'});">清空寄存购物车</a>
					{/if}
					<a class="del" href="javascript:void(0);" onclick="delModel({msg:'确定要清空购物车么？',link:'{url:/simple/clearCart}'});">清空购物车</a>
				</td>
				<td colspan="6" class="t_r">
					{set:$callback = IFilter::act(IReq::get('callback'),'text');}
					<a class="btn_continue" href="{url:$callback}">继续购物</a>
					{if:!empty($goodsIdArray)}
					<a class="btn_pay" href="javascript:check_finish();">去结算</a>
					{/if}
				</td>
			</tr>
		</tfoot>
	</table>

	{if:!empty($goodsIdArray)}
	{set:$buyList = array()}
	{set:$goodsIdStr = join(',',$goodsIdArray)}

	{query:
	name   = order_goods
	fields = count(*) as nums,order_id
	where  = goods_id in ($goodsIdStr)
	group  = order_id
	having = nums > 1
	limit  = 15
	items  = $orderIdResult
	/}

	{if:$orderIdResult}
		{set:$orderIdArray = array()}
		{foreach:items = $orderIdResult item = $orderResultItem}
			{set:$orderIdArray[] = $orderResultItem['order_id']}
		{/foreach}

		{set:$orderIdStr = join(',',$orderIdArray)}
		{query:
		name   = order_goods
		fields = goods_id
		where  = order_id in ($orderIdStr) and goods_id not in ($goodsIdStr)
		limit  = 15
		items  = $goodsIdResult
		/}

		{if:$goodsIdResult}
			{set:$orderGoodsIdArray = array()}
			{foreach:items = $goodsIdResult item = $goodsResultItem}
				{set:$orderGoodsIdArray[] = $goodsResultItem['goods_id']}
			{/foreach}

			{set:$orderGoodsIdStr = join(',',$orderGoodsIdArray)}
			{query:
				name   = goods
				fields = list_img,id,name,sell_price
				where  = id in ($orderGoodsIdStr)
				limit  = 15
				items  = $buyList
			/}
		{/if}
	{/if}

	{if:$buyList}
	<div class="box"><div class="title">购买了同样商品的顾客还购买了</div></div>
		<ul id="scrollpic" class="prolist">
			{foreach:items = $buyList}
			<li>
				<a href="{url:/site/products/id/$item[id]}">
					<img width="98" height="106" src="{webroot:$item[list_img]}" alt="{$item['name']}" title="{$item['name']}">
				</a>
				<p class="pro_title"><a href="{url:/site/products/id/$item[id]}">"{$item['name']}"</a></p>
				<p class="brown"><b>￥{$item['sell_price']}</b></p>
				<label class="btn_orange2"><input type="submit" onclick="window.location.href='{url:/simple/joinCart/link/cart/type/goods/goods_id/$item[id]}';" value="加入购物车"></label>
			</li>
			{/foreach}
		</ul>
		<!--滑动门-->
		<link rel="stylesheet" type="text/css" href="{theme:javascript/jquery.bxSlider/bx_styles/bx_styles.css}" />
		<script type="text/javascript" src="{theme:javascript/jquery.bxSlider/jquery.bxSlider.min.js}"></script>
		<script type="text/javascript">
			jQuery(function(){
				var len = $('#scrollpic li').length > 5 ? 5 : $('#scrollpic li').length;
				$('#scrollpic').bxSlider({controls:true,displaySlideQty:len});
			});
		</script>
	{/if}

	{/if}
	<script type='text/javascript'>

		//购物车数量改动计算
		function cartCount(type,obj,oldCount)
		{
			var countInput    = $('#'+type+'_count_'+obj.id);
			var countInputVal = parseInt(countInput.val());

			//商品数量大于1件
			if(isNaN(countInputVal) || (countInputVal <= 0))
			{
				alert('购买的数量必须大于1件');
				countInput.val(1);
				cartCount(type,obj,oldCount);
			}

			//商品数量小于库存量
			else if(countInputVal > parseInt(obj.store_nums))
			{
				alert('购买的数量不能大于此商品的库存量');
				countInput.val(parseInt(obj.store_nums));
				cartCount(type,obj,oldCount);
			}
			else
			{
				//修改按钮状态
				countInput.attr('disabled',true);
				$('.btn_pay').val('wait');

				//获取之前的购买数量
				if(oldCount == undefined)
				{
					oldCount = countInput.data('oldCount') ? parseInt(countInput.data('oldCount')) : parseInt(obj.count);
				}

				//修改的购买数量
				var changeNum = parseInt(countInput.val()) - oldCount;

				//商品数量没有改动
				if(changeNum == 0)
				{
					//修改按钮状态
					countInput.attr('disabled',false);
					$('.btn_pay').val('ok');
					return '';
				}

				//更新购物车中此商品的数量
				$.getJSON("{url:/simple/joinCart}",{"type":type,"goods_id":obj.id,"goods_num":changeNum,"random":Math.random()},function(content){
					if(content.isError == true)
					{
						alert(content.message);
						var countInput = $('#'+type+'_count_'+obj.id);

						//上次数量回填
						if(changeNum < 0)
						{
							countInput.val(parseInt(countInput.val() - changeNum));
						}
						else
						{
							countInput.val(parseInt(countInput.val() + changeNum));
						}

						//修改按钮状态
						countInput.attr('disabled',false);
						$('.btn_pay').val('ok');
					}
					else
					{
						var countInput = $('#'+type+'_count_'+obj.id);

						//缓存旧的购买数量
						countInput.data('oldCount',parseInt(countInput.val()));

						/*变动的价格可能为负数(减少购买量)*/
						var smallSumC   = (mathSub(parseFloat(obj.sell_price),parseFloat(obj.reduce))) * changeNum; //价格变动
						var weightC     = mathMul(parseFloat(obj.weight),changeNum);       //重量变动
						var originC     = mathMul(parseFloat(obj.sell_price),changeNum);   //原始价格变动
						var discountC   = mathMul(parseFloat(obj.reduce),changeNum);       //优惠变动

						/*开始更新数据(1)*/

						//商品小结计算
						var smallSum    = $('#'+type+'_sum_'+obj.id);
						smallSum.html(mathAdd(parseFloat(smallSum.text()),smallSumC));

						//最终重量
						$('#weight').html(mathAdd(parseFloat($('#weight').text()),weightC));

						//原始金额
						$('#origin_price').html(mathAdd(parseFloat($('#origin_price').text()),originC));

						//优惠价
						$('#discount_price').html(mathAdd(parseFloat($('#discount_price').text()),discountC));

						//促销规则检测
						var final_sum   = mathSub(parseFloat($('#origin_price').text()),parseFloat($('#discount_price').text()));
						var tmpUrl = '{url:/simple/promotionRuleAjax/random/@random@}';
						tmpUrl = tmpUrl.replace("@random@",Math.random());
						$.getJSON( tmpUrl ,{final_sum:final_sum},function(content){
							if(content.promotion.length > 0)
							{
								$('.cart_prompt .indent').remove();

								for(var i = 0;i < content.promotion.length; i++)
								{
									$('.cart_prompt').append('<p class="indent blue">'+content.promotion[i].plan+'，'+content.promotion[i].info+'</p>');
								}
								$('#cart_prompt').show();
							}
							else
							{
								$('.cart_prompt .indent').remove();
								$('#cart_prompt').hide();
							}
							/*开始更新数据 (2)*/

							//促销活动
							$('#promotion_price').html(content.proReduce);

							//最终金额
							$('#sum_price').html(mathSub(mathSub(parseFloat($('#origin_price').text()),parseFloat($('#discount_price').text())),parseFloat($('#promotion_price').text())));

							//修改按钮状态
							countInput.attr('disabled',false);
							$('.btn_pay').val('ok');
						});
					}
				});
			}
		}

		//增加商品数量
		function cart_increase(type,obj)
		{
			//库存超量检查
			var countInput = $('#'+type+'_count_'+obj.id);
			var oldCount   = parseInt(countInput.val());
			if(parseInt(countInput.val()) + 1 > parseInt(obj.store_nums))
			{
				alert('购买的数量大于此商品的库存量');
			}
			else
			{
				if(countInput.attr('disabled') == true)
				{
					return false;
				}
				else
				{
					countInput.attr('disabled',true);
				}
				countInput.val(parseInt(countInput.val()) + 1);
				cartCount(type,obj,oldCount);
			}
		}

		//减少商品数量
		function cart_reduce(type,obj)
		{
			//库存超量检查
			var countInput = $('#'+type+'_count_'+obj.id);
			var oldCount   = parseInt(countInput.val());
			if(parseInt(countInput.val()) - 1 <= 0)
			{
				alert('购买的数量必须大于1件');
			}
			else
			{
				if(countInput.attr('disabled') == true)
				{
					return false;
				}
				else
				{
					countInput.attr('disabled',true);
				}
				countInput.val(parseInt(countInput.val()) - 1);
				cartCount(type,obj,oldCount);
			}
		}

		//检测购买完成量
		function check_finish()
		{
			if($.trim($('.btn_pay').val()) == 'wait')
			{
				window.setInterval("check_finish()", 400);
			}
			else
			{
				window.location.href = '{url:/simple/cart2}';
			}
		}

		/**
		 * 读取购物车
		 */
		function deposit_cart_get()
		{
			$.getJSON('{url:/simple/deposit_cart_get}',{"random":Math.random()},function(json)
			{
				if(json.isError == 1)
				{
					alert('读取购物车失败');
					return;
				}
				//页面刷新
				window.location.reload();
			});
		}
	</script>
</div>
